<?php

class Application_Model_Job extends Application_Model_DomainObject {

    protected $_id;
    /**
     * 任务名称
     */
    protected $_name;
    /**
     * 创建时间
     */
    protected $_dateCreated;
    /**
     * 最后一次执行时间
     */
    protected $_dateLast;
    /**
     * 最后一次执行状态
     */
    protected $_lastStatus;
    /**
     * 是否启用
     */
    protected $_isDisable;
    /**
     * 任务描述
     */
    protected $_description;
    /**
     * 任务类型
     */
    protected $_type;
    /**
     * 执行内容 代码或是url
     */
    protected $_content;
    /**
     * 日期类型
     */
    protected $_dateType;
    /**
     * 执行时间点的限制
     */
    protected $_time;
    /**
     * 执行日期
     */
    protected $_dateValue;
    /**
     * 任务循环执行间隔
     */
    protected $_timeLog;
    /**
     * 任务总执行次数
     */
    protected $_count;
    
    /**
     * 操作人员
     */
    protected $_operator;

    /**
     * 任务类型 - 执行代码
     */
    const Type_Code = 1;
    /**
     * 任务类型 - 执行地址
     */
    const Type_Url = 2;
    /**
     * 启用
     */
    const Disable_Enabled = 1;
    /**
     * 关闭
     */
    const Disable_Close = 2;
    /**
     * 执行时间类型 - 每天
     */
    const Execute_EveryDay = 1;
    /**
     * 执行时间类型 - 周
     */
    const Execute_Week = 2;
    /**
     * 执行时间类型 - 月
     */
    const Execute_Month = 3;
    /**
     * 未执行
     */
    const LastStatus_Default = 0;
    /**
     * 执行成功
     */
    const LastStatus_Succeed = 1;
    /**
     * 执行失败
     */
    const LastStatus_Fail = 2;
   /**
    * 循环执行
    */
    const Execute_ForTime = -1;
    
    public static function getExecuteAll()
    {
        return array(
                   self::Execute_EveryDay => "每天执行"
                ,  self::Execute_Week => "按周执行"
                ,  self::Execute_Month => "按月执行"
        );
    }
    
    public function getExecuteName()
    {
        $executeAll = $this->getExecuteAll();
        return $executeAll[$this->_dateType];
    }
    
    public static function getLastStatusAll()
    {
        return array(
                   self::LastStatus_Default => "未执行过"
                ,  self::LastStatus_Succeed => "执行成功"
                ,  self::LastStatus_Fail => "执行失败"
        );
    }
    
    public function getLastStatusName()
    {
        $lastStatusAll = $this->getLastStatusAll();
        return $this->_lastStatus?$lastStatusAll[$this->_lastStatus]:"未执行过";
    }

    public static function getDisableAll()
    {
        return array(
                   self::Disable_Enabled => "已启用"
                ,  self::Disable_Close => "未启用"
        );
    }
    
    public function getDisableName()
    {
        $disableAll = $this->getDisableAll();
        return $disableAll[$this->_isDisable];
    }

    public static function getTypeAll()
    {
        return array(
                self::Type_Code => "运行代码",
                self::Type_Url => "打开网页"
        );
    }
    
    
    
    public function getTypeName()
    {
        $typeAll = $this->getTypeAll();
        return $typeAll[$this->_type];
    }

    public static function byloginname($username) {
        $where[' name = '] = $username;
        $result = Application_Model_Job::getDao()->queryOne($where);
        return $result;
    }

    public static function getFilter($jobSearch, $pagesize, $pageindex) {
//        var_dump($jobSearch->getSearchFileds());
        return Application_Model_Job::getDao()->queryPage($jobSearch->getSearchFileds(), 'id desc', $pagesize, $pageindex);
    }
    
    public static function getEnabledJob()
    {
        return self::getDao()->queryAll(array("isDisable = "=>  self::Disable_Enabled));
    }
    
    /**
     * 检索当前时间是否执行任务
     */
    public function getNeedRun()
    {
        switch ($this->_dateType) {
            case self::Execute_EveryDay:
                return $this->retrievalDayRun();
            case self::Execute_Week:
                return $this->retrievalWeekRun();   
            case self::Execute_Month:
                return $this->retrievalMonthRun();
            default:
                return false;
        }
    }
    
    private function retrievaForTime()
    {
        $time = strtotime(date("Y-m-d H:i:s"));
        if(!$this->_dateLast){
            return true;
        }
        $lastTime = strtotime(date("Y-m-d H:i:s", strtotime("$this->_dateLast +$this->_timeLog min")));
        if($time >= $lastTime){
            return true;
        }
        return false;
    }
    
    private function retrievalDayRun()
    {
        if($this->_time == -1){
            return $this->retrievaForTime();
        }
        $timeH = date("H");
        if($timeH == $this->_time){
            return true;
        }
        return false;
    }
    
    private function retrievalWeekRun()
    {
        if(!$this->_dateValue){
            return false;
        }
        $arrRunDate = explode(",", $this->_dateValue);
        if(!in_array(date("w"), $arrRunDate)){
            return false;
        }
        return $this->retrievalDayRun();
    }
    
    private function retrievalMonthRun()
    {
        if(!$this->_dateValue){
            return false;
        }
        $arrRunDate = explode(",", $this->_dateValue);
        if(!in_array(date("d"), $arrRunDate)){
            return false;
        }
        return $this->retrievalDayRun();
    }
    
    public function getExecuteTime()
    {
        switch ($this->_dateType) {
            case self::Execute_EveryDay:
                if($this->_time == -1){
                    return "每天每隔".$this->_timeLog."分钟执行一次";
                }
                return "每天准时".$this->_time."点执行一次";
            case self::Execute_Week:
                if($this->_time == -1){
                    return "每周的星期".$this->_dateValue."<br />每隔".$this->_timeLog."分钟执行一次";
                }
                return "每周的星期".$this->_dateValue."<br />每天准时".$this->_time."点执行一次";
            case self::Execute_Month:
                if($this->_time == -1){
                    return "每月的".$this->_dateValue."号<br />每隔".$this->_timeLog."分钟执行一次";
                }
                return "每月的".$this->_dateValue."号<br />每天准时".$this->_time."点执行一次";
            default:
                return false;
        }
    }

}
